package com.uwosh.cs342.g6.mapapp;




import java.io.IOException;
import java.util.concurrent.ExecutionException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import com.uwosh.cs342.g6.mapapp.R;


import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Login extends Activity {
	
	public static int currentUserId = -1;

	public final static String UN = "com.example.se2_mapapp";

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);   
        

    }
	

   /* 
    public boolean onOptionsItemSelected(MenuItem item) {
	    switch(item.getItemId())
	    {
	    case R.id.sendMessage:
	    	Intent sendMessage = new Intent(this, SendMessage.class);
	    	startActivity(sendMessage);
			return true;
	    	

		}
		return true;
	}
    */
    public void register(View v)
    {
    	try {
			boolean success = new RegisterTask().execute(getApplicationContext()).get();
			
			if(success)
			{
				Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
				Intent MainActivity = new Intent(this, MainActivity.class);
//				EditText editText = (EditText) findViewById(R.id.username);
//				String userName = editText.getText().toString();
//				MainActivity.putExtra(UN, userName);
//			    startActivity(MainActivity);
			}
			else
			{
				Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_SHORT).show();
			}
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		} catch (ExecutionException e) {
			
			e.printStackTrace();
		}
    }
    
    public void login(View v)
    {
    	try {
    		User user = new LoginTask().execute(getApplicationContext()).get();
    		
    		currentUserId = user.id;
			

			if(user.id >= 1)
			{
				 
				Toast.makeText(getApplicationContext(), "Success "+user.id, Toast.LENGTH_SHORT).show();
				
				
				DatabaseHandler dbh = new DatabaseHandler(this);
				dbh.insertUser(user);
				dbh.close();
				
				this.finish();
				Intent MainActivity = new Intent(this, MainActivity.class);

			    startActivity(MainActivity);
			    		        
		      
				
			}
			else
			{
				Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_SHORT).show();
			}
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
    }
    
 public class LoginTask extends AsyncTask<Context, Void, User> {
		
		@Override
		protected User doInBackground(Context... params) {
			
			EditText userNameTextBox = (EditText)findViewById(R.id.username);

			String username = userNameTextBox.getText().toString();
			
			EditText passwordTextBox = (EditText)findViewById(R.id.password);

			String password = passwordTextBox.getText().toString();
			
			
			
			HttpClient httpclient = new DefaultHttpClient();
		    HttpPost httppost = new HttpPost("http://softeng.cs.uwosh.edu/students/cs342g6/login.php?" +
		    		"username="+username+"&password="+password);



		    try {

		        // Execute HTTP Post Request

		        ResponseHandler<String> responseHandler = new BasicResponseHandler();
		        String response = httpclient.execute(httppost, responseHandler);
		        
		        int responseInt = Integer.parseInt(response);
		        


				 
		        
		       User user = new User(responseInt,username,password);
		        
	
		        if(responseInt>=1)
		        {	        	
		        	return user;
		        }
		        else
		        {
		        	return null;
		        }

		        //This is the response from a php application


		    } catch (ClientProtocolException e) {
		        //Toast.makeText(params[0], "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
		       
		    } catch (IOException e) {
		        //Toast.makeText(params[0], "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
		     
		    }
			return null;
			
		} // end method doInBackground
		
		@Override
		protected void onPostExecute(User a)
		{
			//Enter Main Message Activity
			
		} // end method onPostExecute

}
    
public class RegisterTask extends AsyncTask<Context, Void, Boolean> {
		
		@Override
		protected Boolean doInBackground(Context... params) {
			
			EditText userNameTextBox = (EditText)findViewById(R.id.username);

			String username = userNameTextBox.getText().toString();
			
			EditText passwordTextBox = (EditText)findViewById(R.id.password);

			String password = passwordTextBox.getText().toString();
			
			
			
			HttpClient httpclient = new DefaultHttpClient();
		    HttpPost httppost = new HttpPost("http://softeng.cs.uwosh.edu/students/cs342g6/register.php?" +
	"usernameRegister="+username+"&passwordRegister="+password);



		    try {

		        // Execute HTTP Post Request

		        ResponseHandler<String> responseHandler = new BasicResponseHandler();
		        String response = httpclient.execute(httppost, responseHandler);
		        int responseInt = Integer.parseInt(response);
		        
		        if(responseInt==1)
		        {
		        	return true;
		        }
		        else
		        {
		        	return false;
		        }

		        //This is the response from a php application


		    } catch (ClientProtocolException e) {
		        return false;
		    	//Toast.makeText(params[0], "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
	
		    } catch (IOException e) {
		    	return false;
		        //Toast.makeText(params[0], "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
		      
		    }
			
		} // end method doInBackground
		
		@Override
		protected void onPostExecute(Boolean a)
		{
		// pass the result Cursor to where it is needed

		} // end method onPostExecute

	}



    
}